package com.likeyoudo.views.adapter.rv;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson2.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.likeyoudo.views.R;
import com.likeyoudo.views.activity.FilePreViewActivity;
import com.likeyoudo.views.activity.ImagePreviewActivity;
import com.likeyoudo.views.activity.public_service.PublicApprovalActivity;
import com.likeyoudo.views.constants.xg.ApprovalConstants;
import com.likeyoudo.views.type.entity.xg.Approval;

import java.util.List;

import cn.hutool.core.util.StrUtil;

/**
 * @author LikeYouDo
 * @date 2023/12/29 11:47
 */
public class ApprovalRvAdapter extends RecyclerView.Adapter<ApprovalRvAdapter.VH> {

    private List<Approval> list;

    Context context;

    public ApprovalRvAdapter(List<Approval> list) {
        this.list = list;
    }

    public List<Approval> getList() {
        return list;
    }

    public void setList(List<Approval> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建复用模板
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View converView = inflater.inflate(R.layout.fragment_public_service_approval_rv, parent, false);
        // 获取上下文
        context = parent.getContext();
        return new VH(converView);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        Approval approval = list.get(position);
        // 设置值
        holder.name.setText(approval.getName());
        holder.nickName.setText(String.format("申请人：%s", approval.getNickName()));
        holder.score.setText(String.format("申请分值：%s", approval.getScore().toString()));
        // 设置state的内容和textColor和背景
        String stateText;
        ColorStateList stateTextColor;
        Drawable stateBackGround;
        switch (approval.getState()) {
            case "2":
                stateText = "待审核";
                stateTextColor = AppCompatResources.getColorStateList(context, com.likeyoudo.common.R.color.state_warning);
                stateBackGround = AppCompatResources.getDrawable(context, com.likeyoudo.common.R.drawable.state_warning);
                break;
            case "3":
                stateText = "通过";
                stateTextColor = AppCompatResources.getColorStateList(context, com.likeyoudo.common.R.color.state_success);
                stateBackGround = AppCompatResources.getDrawable(context, com.likeyoudo.common.R.drawable.state_success);
                break;
            case "4":
                stateText = "拒绝";
                stateTextColor = AppCompatResources.getColorStateList(context, com.likeyoudo.common.R.color.state_danger);
                stateBackGround = AppCompatResources.getDrawable(context, com.likeyoudo.common.R.drawable.state_danger);
                break;
            default:
                // 默认值
                stateText = "待审核";
                stateTextColor = AppCompatResources.getColorStateList(context, com.likeyoudo.common.R.color.state_warning);
                stateBackGround = AppCompatResources.getDrawable(context, com.likeyoudo.common.R.drawable.state_warning);
        }

        holder.state.setText(stateText);
        holder.state.setTextColor(stateTextColor);
        holder.state.setBackground(stateBackGround);

        // 设置具体内容
        holder.tvApprovalContentYear.setText(String.format("%s", approval.getApprovalYear().toString()));
        holder.tvApprovalContentContent.setText(approval.getContent());
        holder.tvApprovalContentDescription.setText(approval.getDescription());
        if (StrUtil.isNotBlank(approval.getOpinion())) {
            holder.tvApprovalContentOpinion.setText(approval.getOpinion());
        } else {
            holder.opinionRow.setVisibility(View.GONE);
        }
        holder.tvApprovalContentScore.setText(String.format("%s", approval.getScore().toString()));

        // 设置点击图片按钮逻辑
        if (StrUtil.isNotBlank(approval.getPictures())) {
            String[] split = approval.getPictures().split(",");
            if (split.length == 0) {
                holder.pictures.setVisibility(View.INVISIBLE);
            } else {
                holder.pictures.setVisibility(View.VISIBLE);
                holder.pictures.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // 跳转到预览图片页面
                        Intent intent = new Intent(context, ImagePreviewActivity.class);
                        intent.putExtra(ApprovalConstants.PICS_KEY, approval.getPictures());
                        ActivityUtils.startActivity(intent);
                    }
                });
            }
        } else {
            holder.pictures.setVisibility(View.INVISIBLE);
        }

        // 设置点击文件
        if (StrUtil.isNotBlank(approval.getFiles())) {
            String[] split = approval.getFiles().split(",");
            if (split.length == 0) {
                holder.files.setVisibility(View.INVISIBLE);
            } else {
                holder.files.setVisibility(View.VISIBLE);
                holder.files.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // 跳转到预览文件页面
                        Intent intent = new Intent(context, FilePreViewActivity.class);
                        intent.putExtra(ApprovalConstants.FILES_KEY, approval.getFiles());
                        ActivityUtils.startActivity(intent);
                    }
                });
            }
        } else {
            holder.files.setVisibility(View.INVISIBLE);
        }

        // 设置点击审批
        holder.tvApproval.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PublicApprovalActivity.class);
                intent.putExtra(ApprovalConstants.DATA_KEY, JSON.toJSONString(approval));
                ActivityUtils.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class VH extends RecyclerView.ViewHolder {

        TextView name, nickName, score, state, tvApprovalContentYear, tvApprovalContentContent,
                tvApprovalContentDescription, tvApprovalContentOpinion, tvApprovalContentScore, pictures, files,tvApproval;
        LinearLayoutCompat opinionRow;

        public VH(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            nickName = itemView.findViewById(R.id.nick_name);
            score = itemView.findViewById(R.id.score);
            state = itemView.findViewById(R.id.state);
            tvApprovalContentYear = itemView.findViewById(R.id.tv_approval_content_year);
            tvApprovalContentContent = itemView.findViewById(R.id.tv_approval_content_content);
            tvApprovalContentDescription = itemView.findViewById(R.id.tv_approval_content_description);
            tvApprovalContentOpinion = itemView.findViewById(R.id.tv_approval_content_opinion);
            tvApprovalContentScore = itemView.findViewById(R.id.tv_approval_content_score);
            opinionRow = itemView.findViewById(R.id.opinion_row);
            pictures = itemView.findViewById(R.id.pictures);
            files = itemView.findViewById(R.id.files);
            tvApproval = itemView.findViewById(R.id.tv_approval);
        }
    }
}
